package com.comum.javahomework420242025.controller;

import com.comum.javahomework420242025.pojo.AskList;
import com.comum.javahomework420242025.pojo.Marketing;
import com.comum.javahomework420242025.pojo.PageBean;
import com.comum.javahomework420242025.pojo.Result;
import com.comum.javahomework420242025.repository.AllFileRepository;
import com.comum.javahomework420242025.repository.MarketingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
public class MarketingController {
    @Autowired
    private AllFileRepository allFileRepository;
    @Autowired
    private MarketingRepository marketingRepository;
    @PostMapping("/addMarketing")
    public Result addMarketing(@RequestBody Marketing marketing){
        marketing.setId(null);

        marketingRepository.save(marketing);
        return Result.success();
    }
    @PutMapping("/updateMarketing")
    public Result updateMarketing(@RequestBody Marketing marketing){
        marketingRepository.save(marketing);
        return Result.success();
    }
    @DeleteMapping("/deleteMarketing")
    public Result deleteMarketing(String id){
        System.out.println(id);
        marketingRepository.deleteById(Long.valueOf(id));
        String type = "公司信息";
        allFileRepository.deleteByElseIdAndType(id,type);
        return Result.success();
    }
    @PostMapping("/getMarketing")
    public Result<PageBean<Marketing>> getMarketing(@RequestBody AskList askList){
        int pageSize = askList.getPageSize();
        int pageNum = askList.getPageNum();
        String str = askList.getStr();
        String type = askList.getFindType();
        PageBean<Marketing> pb = new PageBean<>();
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        Page<Marketing> page = marketingRepository.list(str,pageable);
        pb.setTotal(page.getTotalElements());
        pb.setItems(page.getContent());
        System.out.println(pb);
        return Result.success(pb);


    }

}
